package config

import (
	"fmt"

	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
)

// mysql 配置
type DbConfig struct {
	User    string
	Pass    string
	Host    string
	Port    string
	DbName  string
	Charset string
}

// redis 配置
type RedisConfig struct {
	Host   string
	Port   string
	Pass   string
	DbName int
}

// logger 配置
type LoggerConfig struct {
	Path       string
	MaxSize    int
	MaxBackups int
	MaxAge     int
	Compress   bool
	Level      string
}

// 服务配置
type ServerConfig struct {
	Name    string
	Local   string
	Port    string
	RunMode string
}

// 电子健康卡结构体
type HealthCardConfig struct {
	AppId      string
	Pubkey     string
	SecretKey  string
	HospitalId string
}

// 全局配置，需要哪个添加哪个
type Config struct {
	DB         DbConfig
	REDIS      RedisConfig
	Logger     LoggerConfig
	Server     ServerConfig
	HealthCard map[string]HealthCardConfig
}

const (
	configType = "yaml"
	configPath = "./config/dev.yaml"
)

var Conf = &Config{}

func init() {

	viper.SetConfigFile(configPath)
	viper.SetConfigType(configType)

	err := viper.ReadInConfig()
	if err != nil {
		panic(err)
	}

	err = viper.Unmarshal(&Conf)
	if err != nil {
		panic(err)
	}

	viper.WatchConfig()
	viper.OnConfigChange(func(e fsnotify.Event) {
		err = viper.Unmarshal(&Conf)
		if err != nil {
			panic(err)
		}
	})

	// fmt.Println(Conf.DB)
	// fmt.Println(Conf.REDIS)
	// fmt.Println(Conf.Server)
	// fmt.Println(Conf.Logger)
	fmt.Println(Conf.HealthCard)
}
